/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Project  : MAC-Switcher
 * Package  : my.macswitcher.ui
 * File     : MacSwitcher.java
 * Author   : Lei Duan < lei_d@foxmail.com >
 * Created  : 2011-5-15
 * License  : Apache License 2.0 
 */
package my.macswitcher.ui;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.UIManager;

import my.macswitcher.net.NetProvider;

public class MacSwitcher extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField account;
	private JTextField passwd;
	private JTextField code;
	private JButton button = new JButton("切换");

	private ButtonGroup macGp = new ButtonGroup();
	private JRadioButton mac1 = new JRadioButton("校园有线认证");
	private JRadioButton mac2 = new JRadioButton("校园无线认证");

	private ButtonGroup typeGp = new ButtonGroup();
	private JRadioButton type2 = new JRadioButton("学/工号");
	private JRadioButton type1 = new JRadioButton("校园卡号");

	private NetProvider netProvider = NetProvider.getInstance();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MacSwitcher frame = new MacSwitcher();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MacSwitcher() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		
		setBounds(100, 100, 450, 300);
		setDefaultLookAndFeelDecorated(true);
		// JOptionPane.showMessageDialog(null, "在对话框内显示的描述性的文字", "标题条文字串",
		// JOptionPane.ERROR_MESSAGE);
		try {
//			UIManager
//					.setLookAndFeel("org.fife.plaf.Office2003.Office2003LookAndFeel");
//			 UIManager.setLookAndFeel("org.fife.plaf.OfficeXP.OfficeXPLookAndFeel");
			 UIManager.setLookAndFeel("org.fife.plaf.VisualStudio2005.VisualStudio2005LookAndFeel");
		} catch (Exception e) {
			System.err.println("Oops!  Something went wrong!");
		}
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		macGp.add(mac1);
		macGp.add(mac2);
		mac1.setBounds(275, 36, 121, 23);
		contentPane.add(mac1);
		mac2.setBounds(275, 71, 121, 23);
		contentPane.add(mac2);
		mac1.setSelected(true);

		account = new JTextField();
		account.setBounds(68, 37, 90, 22);
		contentPane.add(account);
		account.setColumns(10);

		JLabel label = new JLabel("账号");
		label.setBounds(25, 32, 30, 29);
		contentPane.add(label);

		JLabel label_1 = new JLabel("登陆选择");
		label_1.setBounds(25, 73, 64, 19);
		contentPane.add(label_1);

		typeGp.add(type1);
		typeGp.add(type2);
		type2.setBounds(68, 91, 121, 23);
		contentPane.add(type2);
		type1.setBounds(68, 116, 121, 23);
		contentPane.add(type1);
		type2.setSelected(true);

		passwd = new JTextField();
		passwd.setBounds(68, 164, 90, 23);
		contentPane.add(passwd);
		passwd.setColumns(10);

		JLabel label_2 = new JLabel("密码");
		label_2.setBounds(25, 166, 30, 19);
		contentPane.add(label_2);

		JLabel lblmac = new JLabel("切换MAC认证选择");
		lblmac.setBounds(247, 10, 90, 23);
		contentPane.add(lblmac);

		JLabel label_3 = new JLabel("输入右侧验证码");
		label_3.setBounds(222, 118, 84, 21);
		contentPane.add(label_3);

		code = new JTextField();
		code.setBounds(264, 165, 90, 22);
		contentPane.add(code);
		code.setColumns(10);

		ImageIcon img = new ImageIcon(this.netProvider.getImage());
		JLabel pics = new JLabel();
		pics.setIcon(img);
		pics.setBounds(316, 120, 80, 19);
		contentPane.add(pics);

		JLabel label_5 = new JLabel("验证码");
		label_5.setBounds(212, 166, 42, 19);
		contentPane.add(label_5);

		button.setBounds(161, 215, 93, 23);
		contentPane.add(button);
		
		JButton about = new JButton("关于");
		about.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "项目地址：http://code.google.com/p/mac-switcher/\n" +
						"版本：1.0 Beta\n"+
						"反馈：<lei_d@foxmail.com>", "关于",
						JOptionPane.OK_OPTION);
				
			}
		});
		about.setBounds(303, 215, 93, 23);
		contentPane.add(about);
		
		button.addActionListener(this);
		
		setTitle("湖南大学:MAC地址切换器--1b by Duan Lei");
		
		//设置居中
		double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		this.setLocation((int) (width - this.getWidth()) / 2,
				(int) (height - this.getHeight()) / 2);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String acc = account.getText();
		String code = this.code.getText();
		String passwd = this.passwd.getText();

		String type = "";
		String mac = "";
		if (this.type1.isSelected()) {
			type = "1";
		}
		if (this.type2.isSelected()) {
			type = "2";
		}
		if (this.mac1.isSelected()) {
			mac = "1";
		}
		if (this.mac2.isSelected()) {
			mac = "2";
		}
		if (acc.equals("") || code.equals("") || passwd.equals("")) {
			JOptionPane.showMessageDialog(null, "必须要填写完整信息", "信息不完整",
					JOptionPane.OK_CANCEL_OPTION);
			return;
		}
		if (e.getSource().equals(button)) {
			netProvider.login(acc, passwd, type, code);
			netProvider.changMAC(mac);
			netProvider.shutDown();
			JOptionPane.showMessageDialog(null, "完成切换！", "完成",
					JOptionPane.ERROR_MESSAGE);
			this.dispose();
		}

	}
}
